package com.gujiayue.common.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import com.gujiayue.common.restful.RestfulRtnInfo;
import com.gujiayue.common.utils.JsonUtils;
import com.gujiayue.common.utils.StringUtils;

@Component
public class CaptchaCodeFilter extends GenericFilterBean {
	private String loginUrl = "/login";

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		response.setContentType("application/json;charset=UTF-8");
//		if ("POST".equals(request.getMethod()) && loginUrl.equals(request.getServletPath())) {
//			// 验证码验证
//			String captcha = request.getParameter("captcha");
//			String genCaptcha = (String) request.getSession().getAttribute("captcha");
//			if (StringUtils.isBlank(captcha)) {
//				PrintWriter out = response.getWriter();
//				out.write(JsonUtils.obeyJson(new RestfulRtnInfo(400, "验证码不能为空!")));
//				out.flush();
//				out.close();
//				return;
//			}
//			if (!genCaptcha.toLowerCase().equals(captcha.toLowerCase())) {
//				PrintWriter out = response.getWriter();
//				out.write(JsonUtils.obeyJson(new RestfulRtnInfo(400, "验证码错误!")));
//				out.flush();
//				out.close();
//				return;
//			}
//		}
		chain.doFilter(request, response);
	}
}